package com.trkj.service.hjj;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.hjj.CalcluateruleDAO_lxlk;
import com.trkj.pojo.Calcluaterule;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor = Exception.class)
public class CalcluateRuleService_lxlk {
	@Autowired
	private CalcluateruleDAO_lxlk calcluateruleDao;
	
	/**
	 *查询全部 
	 */
	public List<Calcluaterule> findAllCalcluate(int page,int rows){
		return calcluateruleDao.findAllCalcluatePage(new PageRequest(page-1, rows));
	}
	
	/**
	 * 模糊查询
	 * @param calcluateruleName
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Calcluaterule> ByNameCalcluate(String calcluateruleName,int page,int rows){
		return calcluateruleDao.findAllCalcluateByName("%"+calcluateruleName+"%", new PageRequest(page-1, rows));
	}
	
	/**
	 * 分页总页数
	 * @return
	 */
	public long CalcluatePageCount(){
		return calcluateruleDao.count();
	}
	
	/**
	 * 新增
	 * @param request
	 * @param calcluateruleName
	 * @param calcluateruleCount
	 * @param calcluateruleRemarks
	 * @param calcluateruleSj
	 * @param calcluateruleState
	 * @return
	 * @throws ServiceException 
	 */
	public boolean addCalcluate(String stainName,String calcluateruleName,String calcluateruleCount,String calcluateruleRemarks,
			Integer calcluateruleSj,Integer calcluateruleState) throws ServiceException{
		
		
		try {
			Calcluaterule calcluate = new Calcluaterule();
			calcluate.setCalcluateruleName(calcluateruleName);
			calcluate.setCalcluateruleCount(calcluateruleCount);
			calcluate.setCalcluateruleSj(new BigDecimal(calcluateruleSj));
			calcluate.setCalcluateruleState(new BigDecimal(calcluateruleState));
			calcluate.setCalcluateruleRemarks(calcluateruleRemarks);
			calcluate.setCalcluaterulePerson(stainName);
			calcluate.setCalcluateruleCjdate(new Timestamp(new Date().getTime()));
			
			calcluateruleDao.save(calcluate);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);			
		}
	}
	
	/**
	 * 修改
	 * @param calcluateruleId
	 * @param calcluate
	 * @return
	 * @throws ServiceException
	 */
	public boolean editCalcluate(Long calcluateruleId,Calcluaterule calcluate) throws ServiceException{
		try {
			Calcluaterule calc = calcluateruleDao.findOne(calcluateruleId);
			calc.setCalcluateruleName(calcluate.getCalcluateruleName());
			calc.setCalcluateruleCount(calcluate.getCalcluateruleCount());
			calc.setCalcluateruleSj(calcluate.getCalcluateruleSj());
			calc.setCalcluateruleState(calcluate.getCalcluateruleState());
			calc.setCalcluateruleRemarks(calcluate.getCalcluateruleRemarks());
			calc.setCalcluateruleUpdatetime(new Timestamp(new Date().getTime()));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);			
		}
	}
	
	/**
	 * 修改状态
	 * @param calcluateruleId
	 * @param calcluateruleState
	 * @return
	 * @throws ServiceException
	 */
	public boolean update_CalcluateruleState(Long calcluateruleId,Integer calcluateruleState) throws ServiceException{
		try {
			Calcluaterule calc = calcluateruleDao.findOne(calcluateruleId);
			calc.setCalcluateruleState(new BigDecimal(calcluateruleState));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);	
		}
	}
	
	/**
	 * 加载
	 * @param calcluateruleId
	 * @return
	 */
	public Calcluaterule loadCalcluaterule(Long calcluateruleId){
		return calcluateruleDao.findOne(calcluateruleId);
	}
	
	/**
	 * 删除
	 * @param calcluateruleId
	 * @throws ServiceException 
	 */
	public int delete_Calcluaterule(Long calcluateruleId) throws ServiceException{
		try {
			Calcluaterule cal = calcluateruleDao.findOne(calcluateruleId);
			if(cal.getKaoqinrules().isEmpty()){
				calcluateruleDao.delete(calcluateruleId);
				return 1;
			}
			return 0;
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
}
